<template>
	<view class="container" :data-theme="theme">
		<view class="header">
			<u--form :model="orderSendForm" ref="orderSendFormRefs" :rules="rules" label-width="80px">
				<u-form-item label="发货类型">
					<!-- <u-radio-group placement="column">
						<u-radio label="普通快递" :activeColor="bgColor">
						</u-radio>
					</u-radio-group> -->
					<label class="radio">
						<radio checked="true" :color="bgColor" />普通快递
					</label>
				</u-form-item>
				<u-form-item label="快递公司" prop="expressCode">
					<uni-data-select v-model="orderSendForm.expressCode" :localdata="options" :clear="false">
					</uni-data-select>
				</u-form-item>
				<u-form-item label="快递单号" prop="expressNumber">
					<u--input v-model="orderSendForm.expressNumber" placeholder="请输入快递单号"></u--input>
				</u-form-item>
				<u-form-item label="分单发货" prop="isSplit">
					<!-- <u-switch :activeColor="bgColor" v-model="orderSendForm.isSplit"></u-switch> -->
					<switch :checked="orderSendForm.isSplit" @change="isSplitChange" :color="bgColor"
						style="transform:scale(0.7)" />
				</u-form-item>
				<u-form-item label=" " label-width="80px">
					<view style="line-height: 20px; color: red;">可选择表格中商品单独发货，发货后会生成新的订单且不能撤回，请谨慎操作！</view>
				</u-form-item>
			</u--form>
			<u-action-sheet :show="expressShow" :actions="options" title="请选择快递公司" @close="expressShow = false"
				@select="expressSelect">
			</u-action-sheet>
		</view>
		<view class="content">
			<!-- 这里是可滚动的内容 -->
			<scroll-view class="scroll-view" scroll-y>
				<view class="scroll-view-inner" v-if="orderSendForm.isSplit"> <!-- 内容区域 -->
					<!-- <u-checkbox-group v-model="checked" placement="row">
						<view class="checke_list">
							循环下面
							<view class="checke_item" v-for="(item, index) in productList" :key="index">
								<view class="item_top">
									<u-checkbox @change="checkbox1(index,$event,item)" :activeColor="bgColor"
										shape="circle" :name="JSON.stringify(item)"></u-checkbox>
									<view class="item_img">
										<u--image :showLoading="true" :src="item.image" width="80px"
											height="80px"></u--image>
									</view>
									<view class="item_right">
										<view><span style="font-weight: bold;">商品名称：</span>{{item.productName}} </view>
										<view><span style="font-weight: bold;">规格：</span>{{item.sku}}</view>
									</view>
								</view>
								<view class="item_bottom" style="font-size: 30rpx;color: #808080;margin: 20rpx 0;">
									<span style="display: inline-block;margin-right: 30rpx;">总数（件）<span
											style="color: #000;">{{item.payNum}}</span></span>
									<span style="display: inline-block;margin-right: 30rpx;">已发<span
											style="color: #000;">{{item.deliveryNum}}</span></span>
									<span>已退款<span style="color: #000;">{{item.refundNum}}</span></span>
								</view>
								<view class="item_bottom">
									<view style="color: #808080;margin-right: 10rpx;">发货数量(件)</view>
									<u-number-box :min="1"
										:max="Number(item.payNum) - Number(item.deliveryNum) - Number(item.refundNum)"
										v-model="item.num"></u-number-box>
									<uni-number-box :min="1"
										:max="Number(item.payNum) - Number(item.deliveryNum) - Number(item.refundNum)"
										v-model="item.num" />
								</view>
							</view>
						</view>
					</u-checkbox-group> -->
					<checkbox-group @change="checkboxGroup">
						<view class="checke_list">
							<!-- 循环下面 -->
							<view class="checke_item" v-for="(item, index) in productList" :key="index">
								<view class="item_top">
									<checkbox :checked="item.checked" :value="JSON.stringify(item)">
									</checkbox>
									<view class="item_img">
										<u--image :showLoading="true" :src="item.image" width="80px"
											height="80px"></u--image>
									</view>
									<view class="item_right">
										<view><span style="font-weight: bold;">商品名称：</span>{{item.productName}} </view>
										<view><span style="font-weight: bold;">规格：</span>{{item.sku}}</view>
									</view>
								</view>
								<view class="item_bottom" style="font-size: 30rpx;color: #808080;margin: 20rpx 0;">
									<span style="display: inline-block;margin-right: 30rpx;">总数（件）<span
											style="color: #000;">{{item.payNum}}</span></span>
									<span style="display: inline-block;margin-right: 30rpx;">已发<span
											style="color: #000;">{{item.deliveryNum}}</span></span>
									<span>已退款<span style="color: #000;">{{item.refundNum}}</span></span>
								</view>
								<view class="item_bottom">
									<view style="color: #808080;margin-right: 10rpx;">发货数量(件)</view>
									<!-- <u-number-box :min="1"
										:max="Number(item.payNum) - Number(item.deliveryNum) - Number(item.refundNum)"
										v-model="item.num"></u-number-box> -->
									<uni-number-box :min="1"
										:max="Number(item.payNum) - Number(item.deliveryNum) - Number(item.refundNum)"
										v-model="item.num" />
								</view>
							</view>
						</view>
					</checkbox-group>
				</view>
			</scroll-view>
		</view>
		<view class="footer">
			<view class="print_footer" v-if="orderSendForm.isSplit">
				<view class="checkAll">
					<!-- <u-checkbox-group v-if="orderSendForm.isSplit">
						<u-checkbox :activeColor="bgColor" shape="circle" v-model="isAllChecked" :checked="isAllChecked"
							@change="onCheckAll" label="全选"></u-checkbox>
					</u-checkbox-group> -->
					<checkbox-group @change="onCheckAll">
						<label class="uni-list-cell uni-list-cell-pd" style="display: flex;line-height: 34rpx;">
							<checkbox :checked="isAllChecked" />全选
						</label>
					</checkbox-group>
				</view>
				<button @click="orderSendCancel" class="button_footer bg_color">取消</button>
				<button @click="orderSendConfirm" class="button_footer bg_color">确认</button>
			</view>
			<view class="print_footer" v-else>
				<button @click="orderSendCancel" class="button_footer bg_color">取消</button>
				<button @click="orderSendConfirm" class="button_footer bg_color">确认</button>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		orderSendApi,
		expressAllApi,
		orderProDetailApi
	} from '@/api/order.js'
	import {
		setThemeColor
	} from '../../../utils/setTheme';
	let app = getApp()
	export default {
		data() {
			return {
				bgColor: "#ffffff",
				theme: app.globalData.theme,
				// 去发货所需要的params参数
				orderSendForm: {
					deliveryType: 'express',
					isSplit: false,
					expressRecordType: '1',
					expressId: '',
					expressCode: '',
					expressNumber: '',
					expressTempId: '',
					toAddr: '',
					toName: '',
					toTel: '',
					orderNo: '',
					detailList: [],
				},
				expressType: 'normal',
				// 验证发货订单表单
				rules: {
					expressNumber: [{
						required: true,
						message: '请选择快递单号',
						trigger: ['blur', 'change']
					}],
					expressCode: [{
						required: true,
						message: '请选择快递公司',
						trigger: ['change']
					}]
				},
				// 下拉框
				options: [],
				expressShow: false, // 快递显示
				productList: [], // 商品信息
				// ------------------>
				isAllChecked: false,
				//	表单收集
				checked: [],
				// printList: [{
				// 	id: 1,
				// 	image: '',
				// 	brand: '鲱鱼',
				// 	productName: '小黄鱼1袋装',
				// 	skuParam: '罐',
				// 	hadPrintNumber: 12,
				// 	orderNumber: 23,
				// 	printNumber: null,
				// 	checked: false,
				// }]
			}
		},
		onLoad(option) {
			this.bgColor = setThemeColor()
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
			// 获取快递类型信息
			this.orderSendForm.orderNo = option.orderNo
			this.expressAll()
			this.orderProDetail(option.orderNo)
		},
		// 监听选中的数组
		watch: {
			checked(newVal, oldVal) {
				if (this.productList.length == this.checked.length) {
					this.isAllChecked = true
				} else {
					this.isAllChecked = false
				}
			},
		},
		methods: {
			// 选择快递公司
			expressSelect(val) {
				console.log("val", val)
			},
			// 改变是否打开分单发货
			isSplitChange(e) {
				this.orderSendForm.isSplit = e.detail.value
			},
			// 全选框
			onCheckAll(val) {
				// if (val) {
				// 	this.checked = this.productList.map((item) => JSON.stringify(item));
				// } else {
				// 	this.checked = []
				// }
				let that = this;
				let isAllCheck = val.detail.value.length > 0;
				if (isAllCheck) {
					this.checked = this.productList.map((item) => JSON.stringify(item));
					for (let item of that.productList) {
						item.checked = true;
					}
				} else {
					this.checked = [];
					for (let item of that.productList) {
						item.checked = false;
					}
				}
			},
			// 单点击复选框事件
			// checkbox1(index, checked, item) {
			// 	this.productList[index].checked = checked;
			// 	if (checked == true) {
			// 		this.checked.push(this.productList[index])
			// 	}
			// },
			// 单点击复选框事件
			checkboxGroup(ele) {
				//-----------
				let checkBoxValue = ele.detail.value
				let checkBoxGroup = checkBoxValue.map((item) => JSON.parse(item));
				let items = this.productList
				let values = checkBoxGroup;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					let item = items[i]
					for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
						if (values[j].id === item.id) {
							this.$set(item, 'checked', true)
							break;
						} else {
							this.$set(item, 'checked', false)
						}
					}
				}
				if (values.length == 0) {
					for (let item of this.productList) {
						item.checked = false;
					}
				}
				this.checked = checkBoxValue;
			},

			// 商品列表
			orderProDetail(orderNo) {
				orderProDetailApi(orderNo).then(async (res) => {

					this.productList = res.data;
					for (let item of this.productList) {
						item.checked = false;
						item.num = 1;
					}
				});
			},

			// 取消按钮
			orderSendCancel() {
				this.orderSendForm = {
						deliveryType: 'express',
						isSplit: false,
						expressRecordType: '1',
						expressId: '',
						expressCode: '',
						expressName: '',
						expressNumber: '',
						expressTempId: '',
						toAddr: '',
						toName: '',
						toTel: '',
						orderNo: '',
						detailList: [],
					},
					this.checked = []
				this.$refs.orderSendFormRefs.resetFields()
				uni.navigateBack({
					delta: 1,
					success(res) {
						uni.$emit("orderFun", false)
					}
				});
			},
			// 确认按钮
			orderSendConfirm() {
				this.$refs.orderSendFormRefs.validate().then(res => {


					if (!this.orderSendForm.isSplit) {
						return this.$util.Tips({
							title: '请打开分单发货'
						});
					}
					if (!(this.checked.length > 0)) {
						return this.$util.Tips({
							title: '至少选择一个商品'
						});
					}
					if (this.checked.length > 0) {
						this.orderSendForm.detailList = this.productList.filter((item) => item.checked).map(({
							num,
							id
						}) => ({
							num,
							orderDetailId: id
						}));
					}
					this.orderSend()
				}).catch(a => {
					this.$util.Tips({
						title: "请填写完整信息"
					})
				})
			},

			// 去发货的功能函数
			async orderSend() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				try {
					await orderSendApi(this.orderSendForm)
					this.orderSendCancel()
					uni.hideLoading()
				} catch (e) {
					uni.hideLoading()
					//TODO handle the exception
					this.$util.Tips({
						title: e
					})
					console.log(e)
				}
			},
			// 查询全部物流公司
			async expressAll() {
				uni.showLoading({
					title: '加载中',
					mask: false
				});
				try {
					let result = await expressAllApi({
						type: this.expressType
					})

					this.options = result.data.map(item => {
						return {
							text: item.name,
							value: item.code
						}
					})
					uni.hideLoading()
				} catch (e) {
					uni.hideLoading()
					//TODO handle the exception
					this.$util.Tips({
						title: e
					})
					console.log(e)
				}
			},
		}
	}
</script>
<style lang="scss">
	page {
		height: 100%;


	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.header {
		height: 300px;
		padding: 20px;
		background-color: white;
	}

	.content {
		flex: 1;
		overflow: hidden;
	}

	.scroll-view {
		height: 100%;
	}

	.scroll-view-inner {

		/* 内容区域背景色 */
		.checke_list {
			width: 100%;
			margin: 20rpx;

			.checke_item {
				display: flex;
				flex-direction: column;
				background-color: white;
				padding: 20px;
				margin-top: 10px;
				width: 100%;
				border-radius: 20rpx;

				.item_top {
					display: flex;
					align-items: center;

					.item_img {
						margin-right: 10px;

						image {
							width: 50px;
							height: 70px;
						}

					}

					.item_right {
						// font-weight: bold;
						line-height: 20px;
					}
				}

				.item_bottom {
					display: flex;
					align-items: center;
					justify-content: flex-end;

					.counter {
						display: flex;
						justify-content: space-between;
						align-items: center;
						width: 100px;
						height: 30px;
						border: 1px solid #ccc;
						border-radius: 5px;
						overflow: hidden;
					}



					.counter text {
						width: 40px;
						height: 30px;
						text-align: center;
						line-height: 30px;
						font-size: 16px;
						color: #333;
					}
				}
			}
		}
	}

	.print_footer {
		display: flex;
		justify-content: space-around;
		align-items: center;
		background-color: white;
		height: 60px;

		view {
			margin-right: 20px;
		}

		.button_footer {
			border-radius: 10rpx;
			padding: 20rpx 40rpx;
			color: #fff;
			@include main_bg_color(theme);
		}
	}
</style>